package com.frejoys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.app.AddUserCashDto;
import com.frejoys.common.dto.admin.UpUserCashDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.enums.EUserCashType;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.util.AppConfigUtil;
import com.frejoys.common.vo.admin.UserCashListVo;
import com.frejoys.common.vo.common.UserCashInfoVo;
import com.frejoys.dao.entity.User;
import com.frejoys.dao.entity.UserCash;
import com.frejoys.dao.mapper.UserCashMapper;
import com.frejoys.dao.mapper.UserMapper;
import com.frejoys.service.ResourceLibraryService;
import com.frejoys.service.UserCashService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserCashServiceImpl extends ServiceImpl<UserCashMapper, UserCash> implements UserCashService {

    private final ResourceLibraryService resourceLibraryService;

    private final UserMapper userMapper;

    @Override
    public boolean addUserCash(AddUserCashDto dto,Long userId) {

        if (dto.getType() == EUserCashType.wxImg.getValue() && StrUtil.isEmpty(dto.getWxImg())){
            throw new GeneralException(HttpStatus.notWxImgError);
        }
//        if (dto.getType() == EUserCashType.wxImg.getValue() && StrUtil.isNotEmpty(dto.getUsername())){
//            throw new GeneralException(HttpStatus.shouldNotAlError);
//        }
//        if (dto.getType() == EUserCashType.wxImg.getValue() && StrUtil.isNotEmpty(dto.getAccount())){
//            throw new GeneralException(HttpStatus.shouldNotAlError);
//        }
        if (dto.getType() == EUserCashType.alipay.getValue() && StrUtil.isEmpty(dto.getUsername())){
            throw new GeneralException(HttpStatus.notUserNameError);
        }
        if (dto.getType() == EUserCashType.alipay.getValue() && StrUtil.isEmpty(dto.getAccount())){
            throw new GeneralException(HttpStatus.notAccountError);
        }
//        if (dto.getType() == EUserCashType.alipay.getValue() && StrUtil.isNotEmpty(dto.getWxImg())){
//            throw new GeneralException(HttpStatus.shouldNotWxError);
//        }
        LambdaQueryWrapper<UserCash> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(UserCash::getId).eq(UserCash::getUserId,userId);
        UserCash cash = getOnly(wrapper);

        if (cash == null){
            //添加用户提现账号信息
            UserCash userCash = new UserCash();
            userCash.setUserId(userId);
            userCash.setType(dto.getType());
            if (StrUtil.isNotEmpty(dto.getWxImg())){
                userCash.setWxImg(dto.getWxImg());
            }
            if (StrUtil.isNotEmpty(dto.getUsername())){
                userCash.setUsername(dto.getUsername());
            }
            if (StrUtil.isNotEmpty(dto.getAccount())){
                userCash.setAccount(dto.getAccount());
            }
            baseMapper.insert(userCash);
        } else {
            LambdaUpdateWrapper<UserCash> updateWrapper = new LambdaUpdateWrapper<>();

            updateWrapper.eq(UserCash::getId,cash.getId())
                    .eq(UserCash::getUserId,userId);
            if (dto.getType() != null){
                updateWrapper.set(UserCash::getType,dto.getType());
            }
            if (StrUtil.isNotEmpty(dto.getWxImg())){
                updateWrapper.set(UserCash::getWxImg,dto.getWxImg());
            }
            if (StrUtil.isNotEmpty(dto.getUsername())){
                updateWrapper.set(UserCash::getUsername,dto.getUsername());
            }
            if (StrUtil.isNotEmpty(dto.getAccount())){
                updateWrapper.set(UserCash::getAccount,dto.getAccount());
            }

            baseMapper.update(updateWrapper);
        }

        return true;
    }


    @Override
    public UserCashInfoVo userCashInfo(Long userId) {
        LambdaQueryWrapper<UserCash> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCash::getUserId,userId);
        UserCash userCash = getOnly(wrapper);

        UserCashInfoVo vo = new UserCashInfoVo();

        if (ObjectUtil.isNotEmpty(userCash)){
            vo.setAccount(userCash.getAccount());
            vo.setUsername(userCash.getUsername());
            vo.setWxImg(userCash.getWxImg());
            vo.setWxImgUrl(AppConfigUtil.getUrlPrefix(userCash.getWxImg()));
            vo.setType(userCash.getType());
        }
        return vo;
    }

    @Override
    public IPage<UserCashListVo> userCashListInfo(PageDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<UserCash> wrapper = new LambdaQueryWrapper<>();
        Page<UserCash> userCashPage = baseMapper.selectPage(page, wrapper);
        IPage<UserCashListVo> userCashs = userCashPage.convert(userCash -> BeanUtil.copyProperties(userCash, UserCashListVo.class));
        List<UserCashListVo> userCashList = userCashs.getRecords();

        if (!userCashList.isEmpty()){
            List<Long> userIds = userCashList.stream().map(UserCashListVo::getUserId).toList();

            //查询用户信息
            List<User> users = userMapper.getUserByIds(userIds);
            Map<Long, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));

            for (UserCashListVo vo: userCashList) {
                if (userMap.containsKey(vo.getUserId())){
                    User user = userMap.get(vo.getUserId());
                    vo.setPhone(user.getPhone());
                    vo.setNickname(user.getNickname());
                }
                vo.setWxImgUrl(AppConfigUtil.getUrlPrefix(vo.getWxImg()));
            }
        }
        return userCashs;
    }

    @Override
    public boolean upUserCash(UpUserCashDto dto) {
        LambdaUpdateWrapper<UserCash> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCash::getId,dto.getId());
        if (StrUtil.isNotEmpty(dto.getAccount())){
            wrapper.set(UserCash::getAccount,dto.getAccount());
        }
        if (StrUtil.isNotEmpty(dto.getUsername())){
            wrapper.set(UserCash::getUsername,dto.getUsername());
        }
        if (StrUtil.isNotEmpty(dto.getWxImg())){
            wrapper.set(UserCash::getWxImg,dto.getWxImg());
        }
        if (dto.getType() != null){
            wrapper.set(UserCash::getType,dto.getType());
        }
        baseMapper.update(wrapper);
        return true;
    }
}
